package control

import (
	"dicomtodb/models"
	"path/filepath"
	"sync"
	"time"

	"github.com/suyashkumar/dicom"
	"github.com/suyashkumar/dicom/pkg/tag"
)

type ConlDicom struct {
	models.FileInfo
	models.DicomInfo
}

func (self *ConlDicom) CheckNull(tag *dicom.Element) string {
	if tag == nil {
		models.Log.WARNING("FileInfoID:%v, tag:%v , tag is null", self.FileInfoID, self.PatientID)
		return ""
	} else {
		models.Log.DEBUG(tag.Tag.String())
		models.Log.DEBUG("FileInfoID:%v,tag:%v is %v", self.FileInfoID, tag.Tag.Element, tag.Value.GetValue().([]string)[0])
		return tag.Value.GetValue().([]string)[0]
	}

}

//解析文件
func (self *ConlDicom) New(dataset *dicom.Dataset, fileid uint) {

	self.FileInfoID = fileid

	//赋值
	PatientID, err := dataset.FindElementByTag(tag.PatientID)
	if err != nil {
		models.Log.ERROR("FileInfoID:%v,Element is tag.PatientID err: %v", self.FileInfoID, err)
		models.Log.FATAL("%v,PatientID~! 解析错误", self.FileName)
		return
	} else {
		self.PatientID = self.CheckNull(PatientID)
	}

	AccessionNumber, err := dataset.FindElementByTag(tag.AccessionNumber)
	if err != nil {
		models.Log.ERROR("FileInfoID:%v,Element is tag.AccessionNumber err: %v", self.FileInfoID, err)
	} else {
		self.AccessionNumber = self.CheckNull(AccessionNumber)
	}

	PatientName, err := dataset.FindElementByTag(tag.PatientName)
	if err != nil {
		models.Log.ERROR("FileInfoID:%v,Element is tag.PatientName err: %v", self.FileInfoID, err)
	} else {
		self.PatientName = self.CheckNull(PatientName)
	}

	SeriesDate, err := dataset.FindElementByTag(tag.SeriesDate)
	if err != nil {
		models.Log.ERROR("FileInfoID:%v,Element is tag.SeriesDate err: %v", self.FileInfoID, err)
	} else {
		self.SeriesDate = self.CheckNull(SeriesDate)
	}

	SeriesTime, err := dataset.FindElementByTag(tag.SeriesTime)
	if err != nil {
		models.Log.ERROR("FileInfoID:%v,Element is tag.SeriesTime err: %v", self.FileInfoID, err)
	} else {
		self.SeriesTime = self.CheckNull(SeriesTime)
	}

	StudyDate, err := dataset.FindElementByTag(tag.StudyDate)
	if err != nil {
		models.Log.ERROR("FileInfoID:%v,Element is tag.StudyDate err: %v", self.FileInfoID, err)
	} else {
		self.StudyDate = self.CheckNull(StudyDate)
	}

	StudyTime, err := dataset.FindElementByTag(tag.StudyTime)
	if err != nil {
		models.Log.ERROR("FileInfoID:%v,Element is tag.StudyTime err: %v", self.FileInfoID, err)
	} else {
		self.StudyTime = self.CheckNull(StudyTime)
	}

	// AcquisitionDate, err := dataset.FindElementByTag(tag.AcquisitionDate)
	// if err != nil {
	// 	models.Log.ERROR("FileInfoID:%v,Element is tag.AcquisitionDate err: %v", self.FileInfoID, err)
	// } else {
	// 	self.AcquisitionDate = self.CheckNull(AcquisitionDate)
	// }

	// AcquisitionTime, err := dataset.FindElementByTag(tag.AcquisitionTime)
	// if err != nil {
	// 	models.Log.ERROR("FileInfoID:%v,Element is tag.AcquisitionTime err: %v", self.FileInfoID, err)
	// } else {
	// 	self.AcquisitionTime = self.CheckNull(AcquisitionTime)
	// }

	StudyInstanceUID, err := dataset.FindElementByTag(tag.StudyInstanceUID)
	if err != nil {
		models.Log.ERROR("FileInfoID:%v,Element is tag.AccessionNumber err: %v", self.FileInfoID, err)
	} else {
		self.StudyInstanceUID = self.CheckNull(StudyInstanceUID)
	}

	SeriesInstanceUID, err := dataset.FindElementByTag(tag.SeriesInstanceUID)
	if err != nil {
		models.Log.ERROR("FileInfoID:%v,Element is tag.SeriesInstanceUID err: %v", self.FileInfoID, err)
	} else {
		self.SeriesInstanceUID = self.CheckNull(SeriesInstanceUID)
	}

	StudyID, err := dataset.FindElementByTag(tag.StudyID)
	if err != nil {
		models.Log.ERROR("FileInfoID:%v,Element is tag.StudyID err: %v", self.FileInfoID, err)
	} else {
		self.StudyID = self.CheckNull(StudyID)
	}

	SeriesNumber, err := dataset.FindElementByTag(tag.SeriesNumber)
	if err != nil {
		models.Log.ERROR("FileInfoID:%v,Element is tag.SeriesNumber err: %v", self.FileInfoID, err)
	} else {
		self.SeriesNumber = self.CheckNull(SeriesNumber)
	}

	InstanceNumber, err := dataset.FindElementByTag(tag.InstanceNumber)
	if err != nil {
		models.Log.ERROR("FileInfoID:%v,Element is tag.InstanceNumber err: %v", self.FileInfoID, err)
	} else {
		self.InstanceNumber = self.CheckNull(InstanceNumber)
	}

	// self.PatientID = PatientID.Value.GetValue().([]string)[0]
	// self.AccessionNumber = AccessionNumber.Value.GetValue().([]string)[0]
	// self.PatientName = PatientName.Value.GetValue().([]string)[0]
	// self.SeriesDate = SeriesDate.Value.GetValue().([]string)[0]
	// self.SeriesTime = SeriesTime.Value.GetValue().([]string)[0]
	// self.StudyDate = StudyDate.Value.GetValue().([]string)[0]
	// self.StudyTime = StudyTime.Value.GetValue().([]string)[0]
	// self.AcquisitionDate = AcquisitionDate.Value.GetValue().([]string)[0]
	// self.AcquisitionTime = AcquisitionTime.Value.GetValue().([]string)[0]
	// self.StudyInstanceUID = StudyInstanceUID.Value.GetValue().([]string)[0]
	// self.SeriesInstanceUID = SeriesInstanceUID.Value.GetValue().([]string)[0]
	// self.StudyID = StudyID.Value.GetValue().([]string)[0]
	// self.SeriesNumber = SeriesNumber.Value.GetValue().([]string)[0]
	// self.InstanceNumber = InstanceNumber.Value.GetValue().([]string)[0]

}

//AnalysisDicom  读取filechan中读取completefile表 的数据，处理完成 并写入 dicomchan中

var Wait sync.WaitGroup

func ThroughData() {
	defer Wait.Done()
loop:
	for {
		select {
		case file, ok := <-FileInfoChan:
			if !ok {
				models.Log.INFO("文件处理完成～！！")
				break loop
			}
			//拼接文件名
			onefile := filepath.Join(file.Dir, file.FileName)
			models.Log.DEBUG("处理 %#v", onefile)
			timenow := time.Now()
			dataset, err := dicom.ParseFile(onefile, nil)
			time2 := time.Now().Sub(timenow)
			models.Log.INFO("FileInfoID:%v 解析时间:%s", file.ID, time2)
			if err != nil {
				models.Log.ERROR("%v", err.Error())
			}
			var d ConlDicom
			//d.New(&dataset, file.ID)
			d.ThroughDicom(&dataset, file.ID)
			// models.Log.INFO("FileInfoID:%v 解析完成", file.ID)
			DicomInofChan <- &d.DicomInfo
			time.Sleep(10 * time.Microsecond)
		default:
			//time.Sleep(100 * time.Microsecond)
		}
	}
}

func AnalysisDicom() {
	defer models.Wait.Done()
	defer models.Log.DEBUG("AnalysisDicom is quit~!!")

	var i uint
	for i = 0; i < models.Config.WorkerThread; i++ {
		models.Log.DEBUG("开启协程；%d", i)
		Wait.Add(1)
		go ThroughData()
	}
	Wait.Wait()
	close(DicomInofChan)

}

//遍历dicom
func (self *ConlDicom) ThroughDicom(dataset *dicom.Dataset, fileid uint) {
	self.FileInfoID = fileid
	for _, d := range dataset.Elements {
		switch d.Tag {
		case tag.PatientID:
			self.PatientID = self.CheckNull(d)
		case tag.AccessionNumber:
			self.AccessionNumber = self.CheckNull(d)
		case tag.PatientName:
			self.PatientName = self.CheckNull(d)
		case tag.SeriesDate:
			self.SeriesDate = self.CheckNull(d)
		case tag.SeriesTime:
			self.SeriesTime = self.CheckNull(d)
		case tag.StudyDate:
			self.StudyDate = self.CheckNull(d)
		case tag.StudyTime:
			self.StudyTime = self.CheckNull(d)
		case tag.AcquisitionDate:
			self.AcquisitionDate = self.CheckNull(d)
		case tag.AcquisitionTime:
			self.AcquisitionTime = self.CheckNull(d)
		case tag.StudyInstanceUID:
			self.StudyInstanceUID = self.CheckNull(d)
		case tag.SeriesInstanceUID:
			self.SeriesInstanceUID = self.CheckNull(d)
		case tag.StudyID:
			self.StudyID = self.CheckNull(d)
		case tag.SeriesNumber:
			self.SeriesNumber = self.CheckNull(d)
		case tag.InstanceNumber:
			self.InstanceNumber = self.CheckNull(d)
		default:
			break
		}

	}

}
